﻿namespace Web.Control.WWW.Answers
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    using System.IO;
    using Lx.Web.Common.ControlsUI;
    using Lx.Web.Common.UI;
    using Model.WWW_WWW.Answers;
    using BLF.WWW_WWW.Answer;
    using System.Collections.Generic;
    using BLF;

    public class QuestionList : BaseControl
    {
        /// <summary>
        /// 显示的类型（1为Most Recent ，2为Most Views ，3为No. of Answers ）
        /// </summary>
        protected int sort = 1;
        /// <summary>
        /// 页面
        /// </summary>
        protected int pageIndex = 1;
        /// <summary>
        /// 目标语言
        /// </summary>
        protected string Learn = "";
        /// <summary>
        /// 使用语言
        /// </summary>
        protected string BaseL = "";
        /// <summary>
        /// Tag名
        /// </summary>
        protected string Tags = "";
        protected Repeater repQuest;
        protected Repeater repTags;
        protected Repeater rptLL;
        /// <summary>
        /// 数据数量
        /// </summary>
        protected int RecordCount = 0;
        protected string sortField = "views";
        protected List<AnswersTagModel> anList;
        protected List<QuestionModel> quList;
        protected override void OnLoad(EventArgs e)
        {
            if (Request["tag"] != "" && !string.IsNullOrEmpty(Request["tag"]))
            {
                Tags = Request["tag"];
                if (Request.Cookies["italkiLearn"] == null)
                {
                    HttpCookie llCookie = new HttpCookie("italkiLearn", "");
                    Response.Cookies.Add(llCookie);
                }
                if (Request.Cookies["italkiSpeak"] == null)
                {
                    HttpCookie blCookie = new HttpCookie("italkiSpeak", "");
                    Response.Cookies.Add(blCookie);
                }
                if (!string.IsNullOrEmpty(Request["sort"]))
                {
                    sort = int.Parse(Request["sort"].ToString());
                }
                if (sort == 3)
                {
                    sortField = "AnswerCount";
                }
                else if (sort == 2)
                {
                    sortField = "views";
                }
                else
                {
                    sortField = "PostDate";
                }
                if (!string.IsNullOrEmpty(Request["page"]))
                {
                    if (Request["page"] == "L")
                    {
                        pageIndex = 999;
                    }
                    else
                    {
                        pageIndex = int.Parse(Request["page"].ToString());
                    }
                }
                if (!string.IsNullOrEmpty(Request["ll"]) && Request["ll"] != "")
                {
                    Learn = Request["ll"];
                    HttpCookie llCookie = new HttpCookie("italkiLearn", Learn);
                    Response.Cookies.Add(llCookie);
                }
                else
                {
                    Learn = Request.Cookies["italkiLearn"].Value;
                }
                if (!string.IsNullOrEmpty(Request["bl"]) && Request["bl"] != "")
                {
                    BaseL = Request["bl"];
                    HttpCookie blCookie = new HttpCookie("italkiSpeak", BaseL);
                    Response.Cookies.Add(blCookie);
                }
                else
                {
                    BaseL = Request.Cookies["italkiSpeak"].Value;
                }

                quList = Answers.GetTagQuestion(Tags.Replace("?", "").Replace("/", "").Replace("-", "").Replace("&", ""), pageIndex, 15, Learn, BaseL, sortField, out RecordCount, out anList);
                int pCount = (int)Math.Ceiling(((decimal)RecordCount) / ((decimal)15));
                if (pageIndex > pCount) pageIndex = pCount;
                if (Tags != "" && Learn != "" && Learn != "all" && BaseL != "" && BaseL != "all")
                {
                    this.DynamicTitle = GetText("AS515", new string[] { Tags, GetLanguageText(Learn), GetLanguageText(BaseL) });
                    this.DynamicKeywords = GetText("AS516", new string[] { Tags, GetLanguageText(Learn), GetLanguageText(BaseL) });
                    this.DynamicDescription = GetText("AS517", new string[] { GetLanguageText(Learn), GetLanguageText(BaseL), Tags });
                }
                else if (Tags != "")
                {
                    this.DynamicTitle = GetText("AS512", new string[] { Tags });
                    this.DynamicKeywords = GetText("AS513", new string[] { Tags });
                    this.DynamicDescription = GetText("AS514", new string[] { Tags });
                }
                if (repQuest != null)
                {
                    repQuest.DataSource = quList;
                    repQuest.DataBind();
                }
                if (repTags != null)
                {
                    repTags.DataSource = anList;
                    repTags.DataBind();
                }
                if (rptLL != null)
                {
                    List<BLF.LanguageObj> lanObjList = new List<BLF.LanguageObj>();
                    lanObjList.AddRange(BLF.StaticData.GetPopLanguageList);
                    rptLL.DataSource = lanObjList;
                    rptLL.DataBind();
                }
                base.OnLoad(e);
            }
            else {
                Response.End();
                return;
            }

        }
        
       
    }
}

